package io.gitee.kotle.android.library.banner.impl.banner

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import io.gitee.kotle.android.adapter.RecyclerViewAdapter
import io.gitee.kotle.android.adapter.RecyclerViewHolder
import io.gitee.kotle.android.library.banner.IBanner
import io.gitee.kotle.android.utils.onClick
import io.gitee.kotle.android.utils.setImageByCoil
import io.gitee.kotle.android.widget.BaseShapeableImageView

/**
 *
 * 描述：
 *
 * 创建人：jiale.wei
 * 创建时间：2021/12/13 4:23 下午
 *
 */
class BannerAdapter<Data : IBanner.BannerData>(
    private var helper: BannerViewHelper<Data>,
    areItemTheSame: ((old: Data?, new: Data?) -> Boolean)? = null,
    areContentsTheSame: ((old: Data?, new: Data?) -> Boolean)? = null
) : RecyclerViewAdapter<Data>(areItemTheSame, areContentsTheSame) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder<Data, *> {
        val customItemLayoutRes = helper.customItemLayoutRes
        val view = if (customItemLayoutRes == null) {
            BaseShapeableImageView(parent.context).also {
                it.scaleType = ImageView.ScaleType.CENTER_CROP
            }
        } else {
            LayoutInflater.from(parent.context).inflate(customItemLayoutRes, parent, false)
        }
        view.layoutParams = RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        ).apply {
            marginStart = helper.itemMargin?.left ?: 0
            topMargin = helper.itemMargin?.top ?: 0
            marginEnd = helper.itemMargin?.right ?: 0
            bottomMargin = helper.itemMargin?.bottom ?: 0
        }
        helper.listener?.onBannerCreated(view)
        return object : RecyclerViewHolder<Data, ViewBinding>(view) {
            init {
                itemView.onClick {
                    val index = bindingAdapterPosition % currentList.count()
                    val data = currentList[index]
                    if (helper.canOnlyClickCurrentItem) {
                        if (helper.viewPage.currentItem == bindingAdapterPosition) {
                            helper.listener?.onBannerClick(itemView, data, index)
                        }
                    } else {
                        helper.listener?.onBannerClick(itemView, data, index)
                    }
                }
            }

            override fun bindData(data: Data) {
                if (itemView is BaseShapeableImageView) {
                    itemView.setImageByCoil(data.getBannerUrl())
                }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return 0
    }

    override fun getItemCount(): Int {
        if (currentList.isEmpty()) {
            return 0
        }
        val repeatCount = helper.repeatCount
        if (repeatCount <= 0) {
            return Int.MAX_VALUE
        }
        return super.getItemCount() * repeatCount
    }

    override fun onBindViewHolder(
        holder: RecyclerViewHolder<Data, *>,
        position: Int,
        payloads: MutableList<Any>
    ) {
        val index = position % currentList.count()
        super.onBindViewHolder(holder, index, payloads)
        helper.listener?.onLoadBanner(holder.itemView, currentList[index], index)
    }
}